package com.earnings.okhttputils.utils.ui.dialog.phoenix.keybottondialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.utils.Md5Utils;
import com.earnings.okhttputils.utils.ui.dialog.phoenix.widget.DrawHookView;
import com.earnings.okhttputils.utils.ui.dialog.phoenix.widget.Keyboard;
import com.earnings.okhttputils.utils.ui.dialog.phoenix.widget.PayEditText;

import java.math.RoundingMode;
import java.text.DecimalFormat;



/**
 * Created by Administrator on 2016/9/8.
 */
public class PswKeyBoardBottomDialog {

    private CustomDialog customDialog;
    private Context context;
    private OnPswKeyBoardResultListener result;
    private String moneyStr;
    private CloasListener cloasListener;
    private boolean isPaying = true;


    public PswKeyBoardBottomDialog(Context context,String moneyStr){
        customDialog = new CustomDialog(context,moneyStr);
        this.moneyStr = moneyStr;
    }

    public void setMoneyStr(String moneyStr) {
        this.moneyStr = moneyStr;
    }

    public void show() {
        customDialog.show();
    }

    public void dismissDialog(){
        if(customDialog != null){
            customDialog.dismiss();
        }
    }

    public void setOnPswKeyBoardResultListener(OnPswKeyBoardResultListener resultListener){
        this.result = resultListener;
    }

    public interface OnPswKeyBoardResultListener{
        void getPswResult(String mResult);
    }

    public  interface CloasListener{
        void cloasClick();
    }

    public void setCloasListener(CloasListener cloasListener){
        this.cloasListener = cloasListener;
    }

    public void showSus(){
        if(customDialog != null){
            customDialog.paySucces();
        }
    }

    public void showFail(){
        if(customDialog != null){
            customDialog.payFail();
        }
    }

    public void showFail(String mesg){
        if(customDialog != null){
            customDialog.payFail(mesg);
        }
    }

    public void showOnError(){
        if(customDialog != null){
            customDialog.payError();
        }
    }
    public void setTitle(String title){
        customDialog.setTitle(title);
       // ((TextView)findViewById(R.id.title)).setText(title);
    }

    private final class CustomDialog extends Dialog {

        private Context context;
        private View viewContent;
        private PayEditText payEditText;
        private Keyboard keyboard;
        private DrawHookView drawHookView;
        private TextView moneyText;
        private String strMoney;
        private RelativeLayout hookRelat;
        private TextView payText;
        private ImageView payStatImg;
        private Button cloasBut;


        public CustomDialog(Context context,String money) {
            super(context, R.style.BottomDialog);
            this.context = context;
            this.strMoney = money;
            init();
            initEvent();

        }
        public void setTitle(String title){

            ((TextView)findViewById(R.id.title)).setText(title);
        }
        private void init() {
            setContentView(R.layout.dialog_keyboardbotton_layouts);
            setCancelable(true);
            setCanceledOnTouchOutside(true);
            getWindow().setGravity(Gravity.BOTTOM);
            getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            moneyText = (TextView) findViewById(R.id.money_text_ids);
            DecimalFormat formater = new DecimalFormat();
            double fltStr = Double.valueOf(strMoney);
            formater.setMaximumFractionDigits(2);
            formater.setGroupingSize(0);
            formater.setRoundingMode(RoundingMode.FLOOR);
            String strfort = String.format("%#.2f", fltStr);
            String rmb = getContext().getResources().getString(R.string.rmb);
            moneyText.setText(rmb + strfort);
            viewContent = findViewById(R.id.view_contents);
            payEditText = (PayEditText) findViewById(R.id.PayEditText_pays);
            keyboard = (Keyboard) findViewById(R.id.KeyboardView_pays);
            drawHookView = (DrawHookView) findViewById(R.id.drawHookViews);
            hookRelat = (RelativeLayout) findViewById(R.id.hook_relat_ids);
            payText = (TextView) findViewById(R.id.pay_typeText_ids);
            payStatImg = (ImageView) findViewById(R.id.pay_stat_img_ids);
            cloasBut = (Button) findViewById(R.id.close_but_ids);

            drawHookView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    loadingFinish();
                    stopLoading();
                }
            });
        }


        private void showLoading() {
            View parentView = (View) viewContent.getParent();
            ViewGroup.LayoutParams lps = parentView.getLayoutParams();
            lps.height = parentView.getHeight();
            parentView.setLayoutParams(lps);

            viewContent.setVisibility(View.GONE);
            hookRelat.setVisibility(View.VISIBLE);
            drawHookView.setVisibility(View.VISIBLE);
            drawHookView.startLoadAnim();
        }

        public void loadingFinish() {
            drawHookView.startFinishAnim();
        }

        public void stopLoading() {
//            viewContent.setVisibility(View.VISIBLE);
//            hookRelat.setVisibility(View.GONE);
            drawHookView.setVisibility(View.GONE);
//            paySucces();
            drawHookView.clearAnim();
        }

        public void paySucces(){
            isPaying = false;
            stopLoading();
            payText.setText("支付成功");
            payStatImg.setImageResource(R.mipmap.success_pay_icon);
            payStatImg.setVisibility(View.VISIBLE);
           // dismissDialog();
        }

        public void payFail(){
            isPaying = true;
            stopLoading();
            payText.setText("验证用户支付密码错误");
            payStatImg.setImageResource(R.mipmap.fail_pay_icon);
            payStatImg.setVisibility(View.VISIBLE);
       //     dismissDialog();
        }

        public void payFail(String msg){
            isPaying = true;
            stopLoading();
            payText.setText(msg);
            payStatImg.setImageResource(R.mipmap.fail_pay_icon);
            payStatImg.setVisibility(View.VISIBLE);
         //   dismissDialog();
        }


        public void payError(){
            isPaying = true;
            stopLoading();
            payText.setText("支付失败,请求接口有失败!");
            payStatImg.setImageResource(R.mipmap.fail_pay_icon);
            payStatImg.setVisibility(View.VISIBLE);
        }

        private void initEvent() {
            keyboard.setOnClickKeyboardListener(new Keyboard.OnClickKeyboardListener() {
                @Override
                public void onKeyClick(int position, String value) {
                    if (position < 11 && position != 9) {
                        payEditText.add(value);
                    } else if (position == 9) {
                        //当点击完成的时候，也可以通过payEditText.getText()获取密码，此时不应该注册OnInputFinishedListener接口
//                        Toast.makeText(context, "您的密码是：" + payEditText.getText(), Toast.LENGTH_SHORT).show();
                    } else if (position == 11) {
                        payEditText.remove();
                    }
                }
            });

            /**
             * 当密码输入完成时的回调
             */
            payEditText.setOnInputFinishedListener(new PayEditText.OnInputFinishedListener() {
                @Override
                public void onInputFinished(String password) {
                    showLoading();
                    if(result != null){

                        result.getPswResult( Md5Utils.getMd5Value( password ));
                    }
                }
            });

            cloasBut.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(isPaying){
                        CustomDialog.this.dismiss();
                    }else{
                        CustomDialog.this.dismiss();
                        if(cloasListener != null){
                            cloasListener.cloasClick();
                        }
                    }
                }
            });
        }

    }


}
